$(function () {
    layui.use('layer', function() {
        var layer = layui.layer;
    })
    $.post("/user/getUser",null,function (info) {
        var obj = JSON.parse(info);
        $("#sallerSpan").html(obj.realName);
    })

    $.post("/customer/findAll",null,function (info) {
        let str = "";
        for(let i=0; i<info.length;i++){
            str += `<option>${info[i].name}</option>`;
        }
        $("#clientSelect").html(str);
    });
});


function addCommodity() {
    layui.use('layer', function() {
        var layer = layui.layer;

        layer.open({
            title:"添加出库商品",
            type:1,
            shadeClose:true,
            skin:'layui-layer-rim',
            area:['800px','600px'],
            anim:4,
            content:$("#addDiv")
        })
    });

    findByItem(1);
}

function findByItem(pageNO) {
    $.post("/Goods/findByItem","pageNO="+pageNO+"&"+$("#findByItemForm").serialize(),function (pageList) {
        var str = "";
        for(var i=0;i<pageList.list.length;i++){
            var obj = pageList.list[i];
            str += `<tr><td>${obj.name}</td>
                    <td>${obj.factory}</td>
                    <td>${obj.salePrice}</td>
                    <td><a class="layui-btn layui-btn-warm" href="javascript:add(${obj.id})">添加</a></td></tr>`;
        }
        $("#allCommodityData").html(str);

        if(pageNO == 1){
            layui.use('laypage', function(){
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'cutPageDiv', //注意，这里的 test1 是 ID，不用加 # 号
                    count: pageList.total, //数据总数，从服务端得到
                    limit:pageList.pageSize,
                    jump:function (obj,first) {
                        if(!first){
                            findByItem(obj.curr)
                        }
                    }
                })
            })
        };
    })
}

function add(id) {
    var param = null;
    if(id != null){
        param = "goodsId="+id;
    }
    $.post("/shop/saleAdd",param,function (shopList) {
        if(shopList != null){
            var str = "";
            for(var i=0;i<shopList.length;i++){
                var obj = shopList[i].goods;
                str += `<tr><td>${obj.name}</td>
                    <td>${obj.factory}</td>
                    <td>${obj.stockNum}</td>
                    <td>${obj.salePrice}</td>
                    <td><input type="number" value="${shopList[i].num}" onblur="changeNum(${obj.id},this.value,${obj.stockNum})"></td>
                    <td><a class="layui-btn layui-btn-warm" href="javascript:delCommodity(${obj.id})">移除商品</a></td></tr>`;
            }
            $("#commodityData").html(str);
        }
        totalPrice();
    });
    layer.closeAll();

}

function changeNum(id,num,stockNum) {
    if(num > stockNum){
        var list = $("#commodityData tr");
        for(var i=0;i<list.length;i++){
            $(list[i]).find("td").eq(4).find("input").val(stockNum);
        }
        num = stockNum;
    }
    $.post("/shop/saleUpdate","goodsId="+id+"&number="+num,function (info) {
        if(info == "ok"){
            totalPrice();
        }
    });
}

function totalPrice() {
    var totalPrice = 0;
    var list = $("#commodityData tr");
    for(var i=0;i<list.length;i++){
        var itemPrice = parseInt($(list[i]).find("td").eq(3).html())*parseInt($(list[i]).find("td").eq(4).find("input").val());
        totalPrice += itemPrice;
    }
    $("#amountSpan").html(totalPrice);
}

function delCommodity(id) {
    $.post("/shop/saleDel","goodsId="+id,function (info) {
        if(info == "ok"){
            add(null);
        }
    })
}

function outOfStock() {
    if(($("#address").val() == "" && $("#address").val().length==0)){
        layer.tips("<span>请输入送货地址</span>", "#addressError");
        return ;
    }else {
        $.post("/saleOrder/sale",
            $("#orderForm").serialize()+"&saller="+$("#sallerSpan").html()+"&amount="+$("#amountSpan").html(),
            function (info) {
                if(info == "ok"){
                    layui.use('layer', function(){
                        var layer = layui.layer;
                        layer.open({
                            type:1,
                            shadeClose:true,
                            skin:'layui-layer-rim',
                            area:['200px','200px'],
                            anim:4,
                            content:$("#outOfStockDiv")
                        });
                    });
                }
            })
    }


}